////////////////////////////////////////////////////////////////////////////////
//  version = '0.1.0'														  //
//  author  = "Eugene Frolov"												  //
//  mail    = "profisphantom@gmail.com"										  //
////////////////////////////////////////////////////////////////////////////////


function main() {
	
	
	// Init Section ////////////////////////////////////////////////////////////
	//Ext.BLANK_IMAGE_URL = 'ext-4.0.0/resources/images/default/s.gif';
	Ext.QuickTips.init();
	config = new ConfigClass();
	server = new rpc.ServiceProxy(config.address, {asynchronous:false, 
			protocol: "XML-RPC"});
	////////////////////////////////////////////////////////////////////////////
	
	
	// Database Section ////////////////////////////////////////////////////////
	Ext.define('MainDB', {
		extend: 'Ext.data.Model',
		fields: [
			'id', 'date', 'client', 'address', 'model', 'disign', 'warranty',
			'ttn', 'schet', 'code', 'comment', 'sn', 'in'
		]
	});
	
	Ext.define('Address', {
		extend: 'Ext.data.Model',
		fields: ['id', 'address']
	});
	
	Ext.define('Client', {
		extend: 'Ext.data.Model',
		fields: ['id', 'name']
	});
	
	Ext.define('Model', {
		extend: 'Ext.data.Model',
		fields: ['id', 'name']
	});
	
	Ext.define('Disign', {
		extend: 'Ext.data.Model',
		fields: ['id', 'name']
	});
	
	Ext.define('Warranty', {
		extend: 'Ext.data.Model',
		fields: ['id', 'periud']
	});
	
	Ext.define('SnInModel', {
		extend: 'Ext.data.Model',
		fields: ['serial_number', 'inventory_number']
	});
	
	Ext.define('GroupModel', {
		extend: 'Ext.data.Model',
		fields: ['id', 'group']
	});
	
	
	group_store = new Ext.data.Store({
		model: 'GroupModel',
		autoLoad: true,
		data: [
			{id: 1, group: 'Группа 1'},
			{id: 2, group: 'Группа 2'},
			{id: 3, group: 'Группа 3'},
			{id: 4, group: 'Группа 4'},
			{id: 5, group: 'Группа 5'},
			{id: 6, group: 'Группа 6'},
			{id: 7, group: 'Группа 7'},
			{id: 8, group: 'Группа 8'},
			{id: 9, group: 'Группа 9'},
			{id: 10, group: 'Группа 10'}
		]
	});

	store = new Ext.data.Store({
		model: 'MainDB',
		autoLoad: true,
		proxy: {
			type: 'ajax',
			url : '/frigoglass/src/server/get_main.py',
			reader: {
				type: 'json',
				root: 'main'
			}
		}
	});
	
	address_store = new Ext.data.Store({
		model: 'Address',
		autoLoad: true,
		proxy: {
			type: 'ajax',
			url: '/frigoglass/src/server/get_table.py?table=Address',
			reader: {
				type: 'json',
				root: 'main'
			}
		}
	});
	
	client_store = new Ext.data.Store({
		model: 'Client',
		autoLoad: true,
		proxy: {
			type: 'ajax',
			url: '/frigoglass/src/server/get_table.py?table=Client',
			reader: {
				type: 'json',
				root: 'main'
			}
		}
	});
	
	model_store = new Ext.data.Store({
		model: 'Model',
		autoLoad: true,
		proxy: {
			type: 'ajax',
			url: '/frigoglass/src/server/get_table.py?table=Model',
			reader: {
				type: 'json',
				root: 'main'
			}
		}
	});
	
	disign_store = new Ext.data.Store({
		model: 'Disign',
		autoLoad: true,
		proxy: {
			type: 'ajax',
			url: '/frigoglass/src/server/get_table.py?table=Disign',
			reader: {
				type: 'json',
				root: 'main'
			}
		}
	});
	
	warranty_store = new Ext.data.Store({
		model: 'Warranty',
		autoLoad: true,
		proxy: {
			type: 'ajax',
			url: '/frigoglass/src/server/get_table.py?table=Warranty',
			reader: {
				type: 'json',
				root: 'main'
			}
		}
	});
	
	sn_in_store = new Ext.data.Store({
		model: 'SnInModel',
		autoLoad: true,
		proxy: {
			type: 'memory',
			url: '/frigoglass/src/server/get_table.py?table=Warranty',
			reader: {
				type: 'json',
				root: 'main'
			}
		}
	});
	
	function getClient(id) {
		return server.get_client(id - 0);
	};
	
	function getAddress(id) {
        return server.get_address(id - 0);
    };
    
    function getModel(id) {
        return server.get_model(id - 0);
    }
    
    function getDisign(id) {
        return server.get_disign(id - 0);
    }
    
    function getWarranty(id) {
        return server.get_warranty(id - 0);
    }
	
	
	fid = {'getClient':   0,
		   'getAddress':  100000,
		   'getModel':    200000,
		   'getDisign':   300000,
		   'getWarranty': 400000};
	
	function resultProxy(func, id) {
		if (window.localStorage) {
			tmp = window.localStorage[fid[func.name] + id];
			if (tmp) {
				return tmp;
			} else {
				tmp = func (id);
				window.localStorage[fid[func.name] + id] = tmp;
				return tmp;
			};
		} else {
			return func(id);
		};
	};
	
	function getClientProxy(id) {
		return resultProxy(getClient, id); 
	}
	
	function getAddressProxy(id) {
		return resultProxy(getAddress, id); 
	}
	
	function getModelProxy(id) {
		return resultProxy(getModel, id); 
	}
	
	function getDisignProxy(id) {
		return resultProxy(getDisign, id); 
	}
	
	function getWarrantyProxy(id) {
		return resultProxy(getWarranty, id); 
	}
	////////////////////////////////////////////////////////////////////////////
	
	// UI Section //////////////////////////////////////////////////////////////
	Ext.create('Ext.Viewport', {
		layout: {
			type: 		'border',
			padding: 	0
		},
		id: 'main_window',
		items:		[{
			region:		'north',
			tbar:	[{
					xtype:	'button',
					text:	'Добавить...',
					iconCls:'icon-add',
					handler: function (){
						Ext.create(Ext.window.Window, {
							title: 'Добавление данных в базу',
							height: 400,
							width: 550,
							autoDestroy: true,
							layout: {
								type: 'table',
								columns: 2
							},
							defaults: {
								labelWidth: 90,
								width:253
							},
							margins:'0 0 0 3',
							bodyStyle  : 'padding: 10px; background-color: #DFE8F6',
							items: [{
								xtype: 'label',
							},{
								xtype: 'datefield',
								fieldLabel: 'Дата отгрузки',
								format: 'd.m.Y',
							},{
								xtype: 'combo',
								fieldLabel: 'Клиент',
								displayField: 'name',
								store: client_store,
								queryMode: 'remote',
								minChars: 1,
								typeAhead: true
							},{
								xtype: 'combo',
								fieldLabel: 'Адрес',
								displayField: 'address',
								store: address_store,
								queryMode: 'remote',
								minChars: 1,
								typeAhead: true
							},{
								xtype: 'combo',
								fieldLabel: 'Модель',
								displayField: 'name',
								store: model_store,
								queryMode: 'remote',
								minChars: 1,
								typeAhead: true
							},{
								xtype: 'combo',
								fieldLabel: 'Дизайн',
								displayField: 'name',
								store: disign_store,
								queryMode: 'remote',
								minChars: 1,
								typeAhead: true
							},{
								xtype: 'combo',
								fieldLabel: 'Гарантия',
								displayField: 'periud',
								store: warranty_store,
								queryMode: 'remote',
								minChars: 1,
								typeAhead: true
							},{
								xtype: 'field',
								fieldLabel: 'ТТН'
							},{
								xtype: 'field',
								fieldLabel: 'Счет'
							},{
								xtype: 'field',
								fieldLabel: 'Код'
							},{
								xtype: 'textarea',
								colspan: 2,
								width: 515,
								hideLabel: true,
							},{
								xtype: 		'grid',
								width: 515,
								height: 100,
								colspan: 2,
								store:		sn_in_store,
								plugins: [Ext.create('Ext.grid.plugin.CellEditing')],
								columns:	[
									Ext.create('Ext.grid.RowNumberer'),
									{text: 'Серийный номер', dataIndex: 'serial_number', flex: 1},
									{text: 'Инвентарный номер', dataIndex: 'inventory_number', flex: 1},
								]
							}],
							bbar:['->', {
								xtype: 'button',
								text: 'Добавить',
							},{
								xtype: 'button',
								text: 'Отмена',
							}]
						}).show();
					}
				},{
					xtype:	'button',
					text:	'Поиск...',
					iconCls:'icon-find',
					handler: function (){
						Ext.create(Ext.window.Window, {
							title: "Поиск данных",
							height: 400,
							width: 550,
							autoDestroy: true,
							layout: {
								type: 'table',
								columns: 5
							},
							id: 'find_window',
							defaults: {
								labelWidth: 90,
								width:100
							},
							margins:'0 0 0 3',
							bodyStyle  : 'padding: 10px; background-color: #DFE8F6',
							items: [{
								xtype: 'label',
								text: 'Группа'
							},{
								xtype: 'label',
								text: 'Колонка'
							},{
								xtype: 'label',
								text: 'Условие'
							},{
								xtype: 'label',
								text: 'Значение'
							},{
								xtype: 'label',
								text: 'Оператор'
							},{
								xtype: 'combo',
								store: group_store,
								displayField: 'group',
								queryMode: 'local',
								minChars: 1,
								typeAhead: true
							},{
								xtype: 'combo',
								store: warranty_store,
								queryMode: 'local',
								minChars: 1,
								width: 150,
								typeAhead: true
							},{
								xtype: 'combo',
								store: warranty_store,
								queryMode: 'local',
								minChars: 1,
								width: 60,
								typeAhead: true
							},{
								xtype: 'field',
								width: 150
							},{
								xtype: 'combo',
								store: warranty_store,
								queryMode: 'local',
								minChars: 1,
								width: 60,
								typeAhead: true
							}],
							bbar:[{
								xtype: 'button',
								text: 'Добавить условие',
								handler: function (){
									x = Ext.getCmp('find_window');
								}
							},'->', {
								xtype: 'button',
								text: 'Поиск',
							},{
								xtype: 'button',
								text: 'Закрыть',
							}]
						}).show();
					}
				},{
					xtype:	'tbfill'
				},{
					xtype:	'button',
					text:	'Помощь',
					iconCls:'icon-help',
					menu:	[{
						text:	'Сообщить об ошибке...',
						iconCls:'icon-bug'
					},{
						text:	'Написать разработчикам...',
						iconCls:'icon-email'
					},'-',{
						text:	'О программе...',
						iconCls:'icon-about'
					}]
				}]
		},{
			region:		'center',
			items:	[{
				xtype: 		'grid',
				height:		'100%',
				store:		store,
				plugins: 	[{
					ptype:		'rowexpander',
					rowBodyTpl : [
						'<p>{comment}</p>'
					]
				}],
				columns:	[
					Ext.create('Ext.grid.RowNumberer'),
					{text: 'Серийный номер', dataIndex: 'sn'},
					{text: 'Инвентарный номер', dataIndex: 'in'},
					{text: 'Название клиента', dataIndex: 'client', renderer: getClientProxy},
					{text: 'Адрес доставки', dataIndex:	'address', renderer: getAddressProxy},
					{text: 'Модель', dataIndex: 'model', renderer: getModelProxy},
					{text: 'Дизайн', dataIndex: 'disign', renderer: getDisignProxy},
					{text: 'Гарантия', dataIndex: 'warranty', renderer: getWarrantyProxy},
					{text: '№ ТТН', dataIndex: 'ttn'},
					{text: '№ с/фактуры', dataIndex: 'schet'},
					{text: 'Склад.код', dataIndex: 'code'},
				] 
			}]
		},{
			region: 		"south",
			items: new Ext.PagingToolbar({
					pageSize: 1000,
					store: store,
					displayInfo: true,
					displayMsg: 'Показано записей {0} - {1} из {2}',
					emptyMsg: "Нет данных..."
			})
		}]
	});
	////////////////////////////////////////////////////////////////////////////
}

Ext.onReady(main);